package dubrouski.csc;

import dubrouski.triangulation.Vector3d;
import dubrouski.triangulation.Triangle;
import java.util.List;
import java.util.Locale;

/**
 *
 * @author Stanislau Dubrouski
 */
class Visualizer {

    public static String drawLCAVectors(List<Shift> vectors, String title, 
            int multiplier) {
        int withError = 0;
        int width = 1024;
        int height = 1024;

        StringBuilder builder = new StringBuilder();

        builder.append(getHeaders(width, height, title));

        StringBuilder linesBuilder = new StringBuilder();
        for (int i = 0; i < vectors.size(); i++) {
            //TODO - resolve exceptions!
            try {
                String line = String.format("<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: black;\"  marker-end=\"url(#myMarker)\"  />",
                        vectors.get(i).getBeginVector().getX(), vectors.get(i).getBeginVector().getY(),
                        vectors.get(i).getEndVector().getX() + (vectors.get(i).getEndVector().getX() - vectors.get(i).getBeginVector().getX()) * multiplier,
                        vectors.get(i).getEndVector().getY() + (vectors.get(i).getEndVector().getY() - vectors.get(i).getBeginVector().getY()) * multiplier);

                linesBuilder.append(line);
                linesBuilder.append(System.getProperty("line.separator"));
            } catch (Exception ex) {
                withError++;
            }
        }

        String lines = linesBuilder.toString().replace(',', '.');

        builder.append(lines);
        builder.append(System.getProperty("line.separator"));
        builder.append(getFooter());
        String result = builder.toString();
        return result;
    }

    public static String drawACAVectors(List<Shift> vectors, String title, 
            int multiplier) {
        int withError = 0;
        int width = 1024;
        int height = 1024;

        StringBuilder builder = new StringBuilder();
        builder.append(getHeaders(width, height, ""));

        StringBuilder linesBuilder = new StringBuilder();

        for (int i = 0; i < vectors.size(); i++) {
            //TODO - resolve exception!
            try {
                String line = String.format("<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: black;\"  marker-end=\"url(#myMarker)\"  />",
                        vectors.get(i).getBeginVector().getY(), height - vectors.get(i).getBeginVector().getZ(),
                        vectors.get(i).getBeginVector().getY(),
                        height - (vectors.get(i).getEndVector().getZ() + (vectors.get(i).getEndVector().getZ() - vectors.get(i).getBeginVector().getZ()) * multiplier));

                linesBuilder.append(line);
                linesBuilder.append(System.getProperty("line.separator"));
            } catch (Exception ex) {
                withError++;
            }
        }

        builder.append(linesBuilder.toString().replace(',', '.'));
        builder.append(getFooter());
        return builder.toString();
    }

    static String drawTriangles(List<Triangle> triangles, String title) {
        int withError = 0;
        int width = 1024;
        int height = 1024;

        StringBuilder builder = new StringBuilder();
        builder.append(getHeaders(width, height, ""));

        StringBuilder linesBuilder = new StringBuilder();
        
        for (int i = 0; i < triangles.size(); i++) {
            //TODO - resolve exception!
            try {
                //use locale in other cases too!
                String line = String.format(Locale.US, "<polygon points=\"%f,%f %f,%f %f,%f\" style=\"fill:none;stroke:lightblue;stroke-width:1\" />",
                        triangles.get(i).getP1().getX(), triangles.get(i).getP1().getY(),
                        triangles.get(i).getP2().getX(), triangles.get(i).getP2().getY(),
                        triangles.get(i).getP3().getX(), triangles.get(i).getP3().getY());

                linesBuilder.append(line);
                linesBuilder.append(System.getProperty("line.separator"));
            } catch (Exception ex) {
                withError++;
            }
        }
        
        
        builder.append(linesBuilder.toString());
        builder.append(getFooter());
        return builder.toString();
    }
    
    public static String drawMultiColoredSpots(List<List<Vector3d>> spots, 
            List<Colors> colors) {
        int withError = 0;
        int width = 1024;
        int height = 1024;

        StringBuilder builder = new StringBuilder();
        builder.append(getHeaders(width, height, ""));

        StringBuilder dotsBuilder = new StringBuilder();

        for (int color = 0; color < colors.size(); color++) {
            for (int i = 0; i < spots.get(color).size(); i++) {
                try {
                    String line = String.format("<circle cx=\"%f\" cy=\"%f\" r=\"10\" fill=\"%s\" />",
                            spots.get(color).get(i).getX(), spots.get(color).get(i).getY(),
                            colors.get(color));

                    dotsBuilder.append(line);
                    dotsBuilder.append(System.getProperty("line.separator"));
                } catch (Exception ex) {
                    withError++;
                }
            }
        }


        builder.append(dotsBuilder.toString().replace(',', '.'));
        builder.append(getFooter());
        return builder.toString();
    }

    private static String getHeaders(int width, int height, String title) {
        StringBuilder builder = new StringBuilder();

        builder.append("<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta charset=\"UTF-8\"/><title></title></head><body><h1></h1>");
        builder.append(System.getProperty("line.separator"));
        builder.append("<svg width=\"" + width + "px\" height=\"" + height + "px\" viewBox=\"0 0 " + width + " " + height + "\">");
        builder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));
        builder.append("<text x=\"0\" y=\"15\" fill=\"Black\">" + title + "</text>");
        builder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));
        builder.append("<defs><marker id=\"myMarker\" viewBox=\"0 0 10 10\" refX=\"1\" refY=\"5\" markerUnits=\"strokeWidth\" orient=\"auto\" markerWidth=\"6\" markerHeight=\"10\"><polyline points=\"0,0 10,5 0,10 1,5\" fill=\"black\" /></marker></defs>");
        builder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));
        builder.append("<rect width=\"" + width + "\" height=\"" + height + "\" style=\"fill: none; stroke: black;\"/>");
        builder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));

        return builder.toString();
    }

    private static String getFooter() {
        return "</svg></body></html>";
    }

    static String drawTrianglesWithVectors(List<Triangle> triangles, 
            String title, List<Shift> vectors, int multiplier) {
        int withError = 0;
        int width = 1024;
        int height = 1024;

        StringBuilder builder = new StringBuilder();
        builder.append(getHeaders(width, height, ""));

        StringBuilder linesBuilder = new StringBuilder();
        
        for (int i = 0; i < triangles.size(); i++) {
            //TODO - resolve exception!
            try {
                //use locale in other cases too!
                String line = String.format(Locale.US, "<polygon points=\"%f,%f %f,%f %f,%f\" style=\"fill:none;stroke:lightblue;stroke-width:1\" />",
                        triangles.get(i).getP1().getX(), triangles.get(i).getP1().getY(),
                        triangles.get(i).getP2().getX(), triangles.get(i).getP2().getY(),
                        triangles.get(i).getP3().getX(), triangles.get(i).getP3().getY());

                linesBuilder.append(line);
                linesBuilder.append(System.getProperty("line.separator"));
            } catch (Exception ex) {
                withError++;
            }
        }
        
        builder.append(linesBuilder.toString());
        linesBuilder = new StringBuilder();
        
        for (int i = 0; i < vectors.size(); i++) {
            //TODO - resolve exceptions!
            try {
                String line = String.format("<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: black;\"  marker-end=\"url(#myMarker)\"  />",
                        vectors.get(i).getBeginVector().getX(), vectors.get(i).getBeginVector().getY(),
                        vectors.get(i).getEndVector().getX() + (vectors.get(i).getEndVector().getX() - vectors.get(i).getBeginVector().getX()) * multiplier,
                        vectors.get(i).getEndVector().getY() + (vectors.get(i).getEndVector().getY() - vectors.get(i).getBeginVector().getY()) * multiplier);

                linesBuilder.append(line);
                linesBuilder.append(System.getProperty("line.separator"));
            } catch (Exception ex) {
                withError++;
            }
        }
        String lines = linesBuilder.toString().replace(',', '.');
        builder.append(lines);        
        builder.append(getFooter());
        return builder.toString();
    }
    
}
